package com.solotiger.okex.controller;


import com.solotiger.okex.common.result.Result;
import com.solotiger.okex.common.result.ResultFactory;
import com.solotiger.okex.config.OkexProcessor;
import com.solotiger.okex.dao.BaseInfoDao;
import com.solotiger.okex.domain.FutureTicker;
import com.solotiger.okex.domain.OkexBaseInfo;
import com.solotiger.okex.service.IOkexService;
import com.solotiger.okex.service.OkexBaseInfoService;
import com.solotiger.okex.service.OkexUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;


@RestController
@RequestMapping("/app")
public class AppController extends AbstractController {

    @Autowired
    private IOkexService okexService;

    @Autowired
    private OkexUserService okexUserService;

    @Autowired
    private OkexBaseInfoService okexBaseInfoService;

    @Autowired
    private OkexProcessor okexProcessor;

    /**
     * @return
     * @throws IOException
     */
    @GetMapping(value = "/futureTicker")
    public Result futureTicker() {
        System.out.println("1111111111111");
        try {
            FutureTicker futureTicker = okexService.futureTicker("btc_usd", "this_week");
            return ResultFactory.INSTANCE.success(futureTicker);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultFactory.INSTANCE.error(e.getMessage());
        }
    }

    /**
     * 启动
     *
     * @return
     * @throws IOException
     */
    @PostMapping(value = "/start")
    public Result start() {
        okexProcessor.stop();
        okexProcessor.start();
        return ResultFactory.INSTANCE.success();
    }

    /**
     * 停止
     *
     * @return
     * @throws IOException
     */
    @PostMapping(value = "/stop")
    public Result stop() {
        okexProcessor.stop();
        return ResultFactory.INSTANCE.success();
    }

    /**
     * 状态
     *
     * @return
     * @throws IOException
     */
    @PostMapping(value = "/getStatus")
    public Result getStatus() {
        return ResultFactory.INSTANCE.success(okexProcessor.getStatus());
    }

    /**
     * 获取
     *
     * @return
     * @throws IOException
     */
    @GetMapping(value = "/getUserInfo")
    public Result getUserInfo() {
        return ResultFactory.INSTANCE.success(getUser());
    }

    /**
     * 获取BaseInfo
     *
     * @return
     * @throws IOException
     */
    @GetMapping(value = "/getBaseInfo")
    public Result getBaseInfo() {
        OkexBaseInfo baseInfo = okexBaseInfoService.findByUserId(getUserId());
        if (baseInfo == null) {
            baseInfo = new OkexBaseInfo();
            baseInfo.setUserId(getUserId());
            okexBaseInfoService.save(baseInfo);
        }
        return ResultFactory.INSTANCE.success(baseInfo);
    }

    /**
     * 修改APIKEY
     *
     * @return
     * @throws IOException
     */
    @PostMapping(value = "/update")
    public Result update(OkexBaseInfo okexBaseInfo) {
        int re = okexBaseInfoService.update(okexBaseInfo);
        if (re > 0) {
            return ResultFactory.INSTANCE.success();
        }
        return ResultFactory.INSTANCE.error("操作失败");
    }
}
